package com.dianrui.qiyouriding.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.qiyouriding.MyApplication;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.dialog.ExitDialog;
import com.dianrui.qiyouriding.event.OpenOrClose;
import com.dianrui.qiyouriding.interfaces.OnAskDialogCallBack;
import com.dianrui.qiyouriding.util.GlideUtil;
import com.dianrui.qiyouriding.util.NoDoubleClickUtils;
import com.dianrui.qiyouriding.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingsActivity extends BaseActivity {

    private GlideUtil glideUtil;

    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.tv_cache_size)
    TextView tvCacheSize;




    @Override
    public int getContentViewId() {
        return R.layout.setting;
    }

    @Override
    public void initView() {
        customInit(true,R.color.main_color);
        title.setText(getString(R.string.settings));
        glideUtil = GlideUtil.getInstance();
        if (null != glideUtil) {
            tvCacheSize.setText(GlideUtil.getCacheSize(this));
        }

    }

    @Override
    public void initData() {}


    @OnClick({R.id.back, R.id.click_about, R.id.click_user,R.id.logout,R.id.click_cache})
    public void onViewClicked(View view) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.click_about:
                startActivity(new Intent(SettingsActivity.this, AboutActivity.class));
                break;
            case R.id.click_user:
                startActivity(new Intent(SettingsActivity.this, WebViewActivity.class).putExtra("type", "useagree"));
                break;

            case R.id.logout:
                ExitDialog.createExitDialog(SettingsActivity.this, getString(R.string.login_out_info), new OnAskDialogCallBack() {
                    @Override
                    public void onokey(Dialog dialog) {
                        dialog.dismiss();
                        SPUtils.getInstance().clear();
                        EventBus.getDefault().post(new OpenOrClose(false));
                        MyApplication.initJiGuangUser("noaligngs", SettingsActivity.this);//绑定推送别名
                        Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.click_cache://清理缓存
                clearCache();
                break;

                default:
                    break;
        }
    }

    private void clearCache() {
        showLoadingDialog("正在清除");
        GlideUtil.clearImageDiskCache(this, new GlideUtil.ClearListener() {
            @Override
            public void onSuccess() {
                dismissLoadingDialog();
                ToastUtil.showToast("清除完成");
                tvCacheSize.setText(GlideUtil.getCacheSize(SettingsActivity.this));
            }

            @Override
            public void onFailure() {
                dismissLoadingDialog();
                ToastUtil.showToast("清除失败,请稍候再试");
            }
        });
    }
}
